package com.pillpoppers.pillbox;

import java.util.ArrayList;
import java.util.Calendar;

import android.os.Bundle;


public class Schedule {

	public static final int PILL_IDS = 0;
	public static final int ACTIVE = 1;
	public static final int TIME = 2;
	public static final int DAY_OF_WEEK = 3;
	public static final int ALARM_BY = 4;

	public static final int ALARM_BY_VIBRATE = 0;
	public static final int ALARM_BY_RING = 1;
	public static final int ALARM_BY_SILENT = 2;

	private int scheduleId;
	private boolean active;
	private int hour;
	private int minute;
	private int dayRepeat; // Sunday to Saturday as 1 - 7
	private int alarmBy;
	private ArrayList<Integer> pillIds;
	
	private String[] pillNames;
	
	public Schedule() {
		scheduleId = 0;
		active = false;
		hour = 8;
		minute = 0;
		dayRepeat = 0;
		alarmBy = 0;
		pillIds = new ArrayList<Integer>();
	}
	
	public Schedule(int scheduleId) {
		this.scheduleId = scheduleId;
		active = false;
		hour = 8;
		minute = 0;
		dayRepeat = Calendar.SUNDAY;
		alarmBy = 0;
		pillIds = new ArrayList<Integer>();
	}
	
	public Schedule(int scheduleId, boolean active, int hour, int minute,
			        int dayRepeat, int alarmBy) {
		this.scheduleId = scheduleId;
		this.active = active;
		this.hour = hour;
		this.minute = minute;
		this.dayRepeat = dayRepeat;
		this.alarmBy = alarmBy;
		pillIds = new ArrayList<Integer>();
	}
	
	public Schedule(Bundle bundle) {
		scheduleId = bundle.getInt("scheduleId");
		active = bundle.getBoolean("active");
		hour = bundle.getInt("hour");
		minute = bundle.getInt("minute");
		dayRepeat = bundle.getInt("dayRepeat");
		alarmBy = bundle.getInt("alarmBy");
		pillIds = new ArrayList<Integer>();
	}
	
	public int getScheduleId() {
		return scheduleId;
	}
	
	public boolean isActive() {
		return active;
	}
	
	public int getHour() {
		return hour;
	}
	
	public int getMinute() {
		return minute;
	}
	
	public int getDayOfWeek() {
		return dayRepeat;
	}
	
	public int getAlarmBy() {
		return alarmBy;
	}
	
	public ArrayList<Integer> getPillIds() {
		return pillIds;
	}
	
	public String[] getPillNames() {
		return pillNames;
	}
	
	public String getFormattedDataString(int id) {
		switch (id) {
		case PILL_IDS:
			return getPillNamesFormattedString();
		case ACTIVE:
			return "Press Save to reflect the change";
		case TIME:
			return getTimeString();
		case DAY_OF_WEEK:
			return getDayString();
		case ALARM_BY:
			return getAlarmByString();
		default: // should never reach here
			return "";
		}
	}
	
	private String getPillNamesFormattedString() {
		/*StringBuilder strBuilder = new StringBuilder();
		for (int i = 0; i < pillNames.length; i++) {
			strBuilder.append(pillNames[i]);
			if (i != pillNames.length - 1)
				strBuilder.append(", ");
		}*/
		return "Pill name";
	}
	
	public String getTimeString() {
		int hourInAmPm = (hour < 13)? hour : hour - 12;
		hourInAmPm = (hour == 0)? 12 : hourInAmPm;
		return convertNumberInto2DigitString(hourInAmPm) + 
				":" + convertNumberInto2DigitString(minute) + " " + ((hour < 12)? "AM" : "PM");
	}
	
	public String getDayString() {
		return AlarmSettingAdapter.DAY_OF_WEEK_OPTIONS[dayRepeat - 1].substring(0, 3);
	}
	
	private String getAlarmByString() {
		return AlarmSettingAdapter.ALARM_BY_OPTIONS[alarmBy];
	}
	
	public void setScheduleId(int scheduleId) {
		this.scheduleId = scheduleId;
	}
	
	public void setActive(boolean active) {
		this.active = active;
	}
	
	public void setHour(int hour) {
		this.hour = hour;
	}
	
	public void setMinute(int minute) {
		this.minute = minute;
	}
	
	public void setDayRepeat(int dayOfWeek) {
		this.dayRepeat = dayOfWeek;
	}
	
	public void setAlarmBy(int alarmBy) {
		this.alarmBy = alarmBy;
	}
	
	public void setPillIds(ArrayList<Integer> pillIds) {
		this.pillIds = pillIds;
	}
	
	public void setPillNames(String[] pillNames) {
		this.pillNames = pillNames;
	}
	
	private String convertNumberInto2DigitString(int num) {
		return (num < 10)? "0" + String.valueOf(num) : String.valueOf(num);
	}
	
	public Bundle getBundle() {
		Bundle bundle = new Bundle();

		bundle.putInt("scheduleId", scheduleId);
		// put pill ids
		bundle.putBoolean("active", active);
		bundle.putInt("hour", hour);
		bundle.putInt("minute", minute);
		bundle.putInt("dayRepeat", dayRepeat);
		bundle.putInt("alarmBy", alarmBy);
		
		return bundle;
	}
	
}
